package de.mhu.demo;

import java.util.LinkedList;
import java.util.List;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode;

import de.mhu.mandalaui.Styleable.EMPHASIS;
import de.mhu.mandalaui.Styleable.SIZE;
import de.mhu.mandalaui.Ui;
import de.mhu.mandalaui.Window;
import de.mhu.mandalaui.actions.AlertInfoAction;
import de.mhu.mandalaui.actions.CleanArrayAttributeAction;
import de.mhu.mandalaui.actions.ClickAction;
import de.mhu.mandalaui.actions.FillArrayAttributeAction;
import de.mhu.mandalaui.actions.InlineAction;
import de.mhu.mandalaui.actions.SequenceAction;
import de.mhu.mandalaui.attributes.AppendAttribute;
import de.mhu.mandalaui.attributes.AppendBooleanAttribute;
import de.mhu.mandalaui.attributes.BooleanAttribute;
import de.mhu.mandalaui.attributes.MultiAttribute;
import de.mhu.mandalaui.attributes.NamedAttribute;
import de.mhu.mandalaui.attributes.NamedBooleanAttribute;
import de.mhu.mandalaui.bind.Call;
import de.mhu.mandalaui.bind.DataProvider;
import de.mhu.mandalaui.bind.DataRow;
import de.mhu.mandalaui.bind.DynamicDataSource;
import de.mhu.mandalaui.bind.DynamicSingleDataSource;
import de.mhu.mandalaui.bind.RemoteAction;
import de.mhu.mandalaui.bind.RemoteActionListener;
import de.mhu.mandalaui.bind.SingleDataProvider;
import de.mhu.mandalaui.layout.HorizontalLayout;
import de.mhu.mandalaui.layout.Panel;
import de.mhu.mandalaui.layout.VerticalLayout;
import de.mhu.mandalaui.list.SelectableList;
import de.mhu.mandalaui.list.ListItem;
import de.mhu.mandalaui.list.StaticSelectableList;
import de.mhu.mandalaui.menu.Menu;
import de.mhu.mandalaui.menu.MenuDivider;
import de.mhu.mandalaui.menu.MenuItem;
import de.mhu.mandalaui.properties.BooleanProperty;
import de.mhu.mandalaui.properties.PropertiesTable;
import de.mhu.mandalaui.properties.SelectProperty;
import de.mhu.mandalaui.properties.StringProperty;
import de.mhu.mandalaui.table.Table;
import de.mhu.mandalaui.table.TableHeader;
import de.mhu.mandalaui.table.TableRow;
import de.mhu.mandalaui.text.Text;
import de.mhu.mandalaui.widgets.Button;
import de.mhu.mandalaui.widgets.ButtonGroup;
import de.mhu.mandalaui.widgets.DropdownButton;

public class Demo01 {

	public Window createWindow(String id) {
		Window window = Ui.createWindow(id);
		
		// base structure
		
		VerticalLayout mainLayout = new VerticalLayout();
		window.setContent(mainLayout);
		
		HorizontalLayout layout = new HorizontalLayout();
		mainLayout.add(layout);
		
		Panel left = new Panel("Left");
		layout.add(left, new HorizontalLayout.Options(3));
		
		Panel right = new Panel("Right");
		layout.add(right, new HorizontalLayout.Options(9));
		
		PropertiesTable userDetails = new PropertiesTable();
		
		right.add(userDetails);
		
		userDetails.add(new StringProperty("firstname","first"), new PropertiesTable.Options("First Name"));
		userDetails.add(new StringProperty("lastname","last"), new PropertiesTable.Options("Last Name"));
		userDetails.add(new BooleanProperty("active","active"), new PropertiesTable.Options("Aktiv"));
		SelectProperty salutation = new SelectProperty("salutation", "", userDetails.getDataAttribute());
		userDetails.add(salutation, new PropertiesTable.Options("Anrede"));
		
		DynamicSingleDataSource userDetailsData = new DynamicSingleDataSource();
		userDetailsData.setTarget(userDetails.getDataAttribute());
		userDetailsData.setProvider(new SingleDataProvider() {
			
			@Override 
			public DataRow getData(JsonNode in) {
				DataRow out = null;
				out = new DataRow("firstname", "Donald", "lastname", "Duck", "id", 1, "salutation", "mr", "active", true );
				return out;
			}
		});
		
		DynamicDataSource salutationData = new DynamicDataSource();
		salutationData.setTarget(salutation.getOptions());
		salutationData.setProvider(new DataProvider() {
			
			@Override
			public void fillWithData(JsonNode in, List<DataRow> data) {
				data.add(new DataRow("ms", "Frau"));
				data.add(new DataRow("mr", "Herr"));
			}
		});
		
		window.getInitialAction().getActions().add(salutationData);
		window.getInitialAction().getActions().add(userDetailsData);
		
		
		SelectableList list = new SelectableList();
		left.add(list);

		DynamicDataSource userListData = new DynamicDataSource();
		userListData.setTarget(list.getDataAttribute());
		userListData.setProvider(new DataProvider() {
			
			@Override
			public void fillWithData(JsonNode in, java.util.List<DataRow> data) {
				data.add(new DataRow("name", "Hans","id",1));
				data.add(new DataRow("name", "Peter","id",2));
				data.add(new DataRow("name", "Lars","id",3));
				data.add(new DataRow("name", "Sabine","id",4));
				data.add(new DataRow("name", "Max","id",5));
			}
		});
		window.getInitialAction().getActions().add(userListData);
		
		ListItem item = new ListItem();
		Text heading = new Text(Text.TYPE.H4,"");
		heading.setBindValue(new NamedAttribute("name"));
		item.add(heading);
		
		list.setItem(item);
	
		list.setSelectAction(new AlertInfoAction());
		
		return window;
	}
}
